package com.shuang.controller;

import com.shuang.pojo.Dept;
import com.shuang.pojo.Result;
import com.shuang.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 部门管理
* */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /*
    * 查询部门列表
    * */
    @GetMapping
    public Result findAll() {
        log.info("查询全部部门信息");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /*
    * 根据前端传参id的值 删除部门
    * */
    @DeleteMapping
    public Result deleteById(Integer id) {
        log.info("删除部门：" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    * 新增部门
    * */
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门：" + dept);
        deptService.addDept(dept);
        return Result.success();
    }

    /*
    * 根据id查询部门
    * */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询部门：" + id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    /*
    * 修改部门
    * */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改的部门：" + dept);
        deptService.update(dept);
        return Result.success();
    }
}
